package com.example.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class PriorityMessageProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送不同优先级的订单处理消息
     */
    public void sendOrderMessages() {
        // 普通用户订单 - 低优先级
        sendOrderMessage("普通用户订单#001", 1);
        
        // VIP用户订单 - 中等优先级
        sendOrderMessage("VIP用户订单#002", 2);
        
        // 紧急订单 - 高优先级
        sendOrderMessage("紧急订单#003", 3);
        
        // 系统故障订单 - 最高优先级
        sendOrderMessage("系统故障订单#004", 4);
        
        // 普通用户订单 - 低优先级
        sendOrderMessage("普通用户订单#005", 5);
        
        // VIP用户订单 - 中等优先级
        sendOrderMessage("VIP用户订单#006", 4);
        
        // 紧急订单 - 高优先级
        sendOrderMessage("紧急订单#007", 5);
    }

    private void sendOrderMessage(String orderInfo, int priority) {
        // 设置消息属性，包括优先级
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setPriority(priority);
        messageProperties.setContentType("text/plain");
        
        Message message = new Message(orderInfo.getBytes(), messageProperties);
        
        rabbitTemplate.send("order.priority.exchange", "order.priority", message);
        log.info("已发送订单消息: {}，优先级: {}", orderInfo, priority);
    }
}
